package com.zlkj.ssm.shop.core.config;
import java.io.Serializable;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import com.zlkj.ssm.shop.core.cache.provider.EhcacheCacheProvider;
import com.zlkj.ssm.shop.core.cache.provider.RedisCacheProvider;
import com.zlkj.ssm.shop.core.cache.provider.SystemManager;
/**
 *    基于普通的Cache实现
 */
/**
 *@PropertySource 注解当前类，参数为对应的配置文件路径，这种方式加载配置文件，可不用在xml中配置PropertiesFactoryBean
 *				     引入jdbc.properties，使用时方便得多，DataSourceConfiguration不再需要成员变量，
 *				     取而代之的是需要注入一个Environment环境配置，使用env.getProperty(key)获取数据：
 */
@Configuration
public class CacheConfiguration {
	@Bean
	public EhcacheCacheProvider initEhcacheCacheProvider() {
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		EhcacheCacheProvider provider = new EhcacheCacheProvider();
		provider.setCacheName("eternalCache");
		provider.setConfigLocation(resolver
				.getResource("classpath:ehcache.xml"));
		return provider;
	}
	
	@Bean(name="systemManager")
	public SystemManager initSystemManager(){
		SystemManager systemManager=new SystemManager();
		systemManager.setCacheProvider(initEhcacheCacheProvider());
		return systemManager;
	}
	@Bean
	@ConfigurationProperties(prefix="spring.redis")
    public RedisConnectionFactory jedisConnectionFactory(){
   /*     
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(Integer.valueOf(env.getProperty("spring.redis.maxidle")));
        poolConfig.setMinIdle(Integer.valueOf(env.getProperty("spring.redis.minIdle")));
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);
        poolConfig.setTestWhileIdle(true);
        poolConfig.setNumTestsPerEvictionRun(Integer.valueOf(env.getProperty("spring.redis.numtestsperevictionrun")));
        poolConfig.setTimeBetweenEvictionRunsMillis(Long.valueOf(env.getProperty("spring.redis.betweenEvictionrunsmillis")));
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);
        jedisConnectionFactory.setHostName(env.getProperty("spring.redis.host"));
        jedisConnectionFactory.setPassword(env.getProperty("spring.redis.password"));
        jedisConnectionFactory.setPort(Integer.valueOf(env.getProperty("spring.redis.port")));
        return jedisConnectionFactory;
     */
        return new JedisConnectionFactory();
    }
    @Bean
    public RedisTemplate<String, Serializable> redisTemplate() {
    	RedisTemplate<String, Serializable> redisTemplate=new RedisTemplate<String, Serializable>();
    	redisTemplate.setConnectionFactory(jedisConnectionFactory());
        return redisTemplate;
    }   
    @Bean
    public RedisCacheProvider  redisCacheProvider(){
    	RedisCacheProvider redisCacheProvider=new RedisCacheProvider();
    	redisCacheProvider.setRedisTemplate(redisTemplate());
    	return redisCacheProvider;
    }
}
